/**
 * @(#)BaseActionSupport.java 2011-9-29
 * 
 * Copyright 2000-2011 by ChinanetCenter Corporation.
 *
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of
 * ChinanetCenter Corporation ("Confidential Information").  You
 * shall not disclose such Confidential Information and shall use
 * it only in accordance with the terms of the license agreement
 * you entered into with ChinanetCenter.
 * 
 */

package com.szf.base.web;

import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.szf.base.dao.Page;

public abstract class BaseActionSupport {

	protected Logger logger = LoggerFactory.getLogger(getClass());

	protected static final String SUCCESS = "success";
	protected static final String INPUT = "input";

	protected HttpServletRequest request;
	protected HttpServletResponse response;
	protected Locale locale;
	protected Principle principle;
	protected String method;

	public void setServletRequest(HttpServletRequest request) {
		this.request = request;
	}

	public void setServletResponse(HttpServletResponse response) {
		this.response = response;
	}

	public void setLocale(Locale locale) {
		this.locale = locale;
	}

	public void setPrinciple(Principle principle) {
		this.principle = principle;
	}

	public Principle getPrinciple() {
		return principle;
	}

	public Locale getLocale() {
		return locale;
	}

	/**
	 * default request
	 */
	public String execute() {
		return SUCCESS;
	}

	protected void ajaxDefaultResponse() {
		WebUtil.renderJson(response, new AjaxResultInfo(true, "success"));
	}

	protected void ajaxErrorResponse(String msg) {
		WebUtil.renderJson(response, new AjaxResultInfo(false, msg));
	}

	protected static HttpServletRequest warpRequest(HttpServletRequest request) {
		Map param = new HashMap(request.getParameterMap());
		int pageSize = Integer.parseInt(((String[]) param.get("rows"))[0]);
		int pageNum = Integer.parseInt(((String[]) param.get("page"))[0]);
		if (param.containsKey("sort") && param.containsKey("order")) {
			String sort = ((String[]) param.get("sort"))[0];
			String order = ((String[]) param.get("order"))[0];
			param.put("q_sortField_eq_s", sort);
			param.put("q_orderType_eq_s", order);
			param.remove("sort");
			param.remove("order");
		}
		param.remove("page");
		param.remove("rows");
		JSONObject pageInfo = new JSONObject();
		JSONObject page = new JSONObject();
		page.put("pageSize", pageSize);
		page.put("pageNum", pageNum);
		page.put("startRowNum", (pageNum - 1) * pageSize + 1);
		pageInfo.put("pageInfo", page);
		param.put("_gt_json", pageInfo.toString());
		return new ParameterRequestWrapper(request, param);
	}

	protected static JSONObject wrapResult(Page page) {
		JSONObject rs = new JSONObject();
		rs.put("total", page.getRecordCount());
		rs.put("rows", page.getQueryList());
		return rs;
	}

	public String getMethod() {
		return method;
	}

	public void setMethod(String method) {
		this.method = method;
	}

}
